﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UIElements.ViewModel;

namespace UIElements
{
    /// <summary>
    /// Interaction logic for MainControl.xaml
    /// </summary>
    public partial class MainControl : UserControl
    {
        #region Private Fields

        private MainControlViewModel viewModel;
        private EnvDTE.DTE dte;

        #endregion

        #region Constructors

        public MainControl()
        {
            InitializeComponent();

            // initialize view model
            InitializeViewModel();
        }

        public MainControl(EnvDTE.DTE dte)
        {
            InitializeComponent();

            // set DTE
            this.dte = dte;

            // initialize view model
            InitializeViewModel();
        }

        #endregion

        #region Helpers

        private void InitializeViewModel()
        {
            // initialize
            viewModel = new MainControlViewModel(this, dte);

            // set data context to view model
            this.DataContext = viewModel;

            // register event handlers for tvPatternExplorer
            this.tvPatternExplorer.MouseRightButtonUp +=
                (sender, eventArgs) => { (this.DataContext as MainControlViewModel).tvPatternExplorer_OnMouseRightButtonUp(sender, eventArgs); };
            this.tvPatternExplorer.PreviewMouseRightButtonDown +=
                (sender, eventArgs) => { (this.DataContext as MainControlViewModel).tvPatternExplorer_OnPreviewMouseRightButtonDown(sender, eventArgs); };
            this.tvPatternExplorer.MouseLeftButtonUp +=
                (sender, eventArgs) => { (this.DataContext as MainControlViewModel).tvPatternExplorer_OnMouseLeftButtonUp(sender, eventArgs); };
            this.tvPatternExplorer.SelectedItemChanged +=
                (sender, eventArgs) => { (this.DataContext as MainControlViewModel).tvPatternExplorer_OnSelectedItemChanged(sender, eventArgs); };

            // register event handlers for lbxPatternLocation
            //this.lbxPatternLocation.MouseRightButtonUp +=
            //    (sender, eventArgs) => { (this.DataContext as MainControlViewModel).lbxPatternLocation_OnMouseRightButtonUp(sender, eventArgs); };
            //this.lbxPatternLocation.PreviewMouseRightButtonDown +=
            //    (sender, eventArgs) => { (this.DataContext as MainControlViewModel).lbxPatternLocation_OnPreviewMouseRightButtonDown(sender, eventArgs); };
            //this.lbxPatternLocation.MouseLeftButtonUp +=
            //    (sender, eventArgs) => { (this.DataContext as MainControlViewModel).lbxPatternLocation_OnMouseLeftButtonUp(sender, eventArgs); };
        }

        private void tbxEditableText_KeyDown(object sender, KeyEventArgs e)
        {
            viewModel.tbxEditableText_KeyDown(sender, e);
        }

        private void tbxTemplateEditableText_KeyDown(object sender, KeyEventArgs e)
        {
            viewModel.tbxTemplateEditableText_KeyDown(sender, e);
        }

        private void RenameMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.RenameMenuItem_Click(sender, e);
        }

        private void EditMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.EditMenuItem_Click(dte);
        }

        private void GenerateCodeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.GenerateCodeMenuItem_Click(dte);
        }

        private void ViewCodeMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.ViewCodeMenuItem_Click(dte);
        }

        private void AddNewInstanceMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.AddNewInstanceMenuItem_Click(dte);
        }

        private void RemoveInstanceMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.RemoveInstanceMenuItem_Click(dte);
        }

        private void RemoveTemplateMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.RemoveTemplateMenuItem_Click(dte);
        }

        private void ReplicateMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.ReplicateMenuItem_Click(dte);
        }

        private void NewLocationMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.NewLocationMenuItem_Click();
        }

        private void UseInCurrentProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.UseInCurrentProjectMenuItem_Click(dte);
        }

        private void RefreshMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.RefreshMenuItem_Click(dte);
        }

        private void AddNewTemplateMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.AddNewTemplateMenuItem_Click(dte);
        }

        private void EditTemplateMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.EditTemplateMenuItem_Click(dte);
        }

        private void PropagateChangesMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.PropagateChangesMenuItem_Click(dte);
        }

        private void CompileMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.CompileMenuItem_Click(dte);
        }

        private void ShowAllPatternsMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.ShowAllPatternsMenuItem_Click(dte);
        }

        private void PropagateInstanceChangesMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.PropagateInstanceChangesMenuItem_Click(dte);
        }

        private void AddTemplateDescriptionMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.AddTemplateDescriptionMenuItem_Click(dte);
        }

        private void AddInstanceDescriptionMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.AddInstanceDescriptionMenuItem_Click(dte);
        }

        private void btnSaveDescription_Click(object sender, RoutedEventArgs e)
        {
            viewModel.BtnSaveDescription_Click(dte);
        }

        private void RemoveFromCurrentProjectMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viewModel.RemoveFromCurrentProjectMenuItem_Click(dte);
        }

        #endregion 
    }
}
